﻿using Ldt.SettingCenter.Core;
using LDT.Task.JudgetDoc.Generation.DataBase;
using LdtJudge.DataEngine.CallInterface;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.CallInterface.ConfigManager;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.CommonWordHandle;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.From.Control;
using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.CommModel;
using LdtJudge.DataEngine.Model.OptimizationModel;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ldt.WindowsFormsApp
{
    public partial class FormTestData : Form
    {
        private bool isdeletewordpath;

        public FormTestData()
        {
            InitializeComponent();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (uiTextBox1.Text.Trim().Length>0 && uiTextBox2.Text.Trim().Length > 0 && uiTextBox3.Text.Trim().Length > 0 && uiTextBox4.Text.Trim().Length > 0 && uiTextBox6.Text.Trim().Length > 0)
            {
                ChangeDataModel changeDataModel = new ChangeDataModel();
                changeDataModel.ChangeAttrName = "";
                changeDataModel.ChangeType = "a_node";
                changeDataModel.ChangeValue = HttpHelp.EncodeBase64(uiTextBox4.Text.Trim());
                changeDataModel.NodeId = "0";
                changeDataModel.XmlPath = "案件要素中心";
                RootModel rootModel = new RootModel();
                rootModel.changedata = new List<ChangeDataModel>();
                rootModel.changedata.Add(changeDataModel);
                string message= JsonConvert.SerializeObject(rootModel);

                FillBackOptParameterModel fillBackOptParameterModel = new FillBackOptParameterModel();
                //fillBackOptParameterModel.AppId = "app";
                //fillBackOptParameterModel.CaseID = uiTextBox6.Text.Trim();
                //fillBackOptParameterModel.CaseType = uiTextBox1.Text.Trim();
                //fillBackOptParameterModel.CCIDs = "";
                //fillBackOptParameterModel.Content = HttpHelp.EncodeBase64(message);
                //fillBackOptParameterModel.CourtID = "";
                //fillBackOptParameterModel.laststage = uiTextBox2.Text.Trim();
                //fillBackOptParameterModel.MsgId = "66666";
                //fillBackOptParameterModel.MsgState = "1";
                //fillBackOptParameterModel.Outrelatid = uiTextBox6.Text.Trim();
                //fillBackOptParameterModel.SourceCode = uiTextBox3.Text.Trim();
                //fillBackOptParameterModel.SystemCode = uiTextBox3.Text.Trim();
                string messagexx = JsonConvert.SerializeObject(fillBackOptParameterModel);
                FillBackOpt(messagexx);

                MessageBox.Show("执行完毕");
            }
            else
            {
                MessageBox.Show("请输入完整的信息！！！！");
            }
        }

        private IConfigRetrieval _configRetrieval { get; set; }
        public void FillBackOpt(string message)
        {

            IConfigRetrieval configRetrieval;
            var _configuration = new ConfigurationBuilder().SetBasePath(System.AppContext.BaseDirectory).AddJsonFile("appsettings.json", true, true).Build();
            ConfigManagerHandle configManagerHandle = new ConfigManagerHandle(_configuration);
            //settingCenterUrl = configManagerHandle.Settingcenter;ConfigManagerHandle.Appcode
            ISettingCenter settingCenter = (ISettingCenter)DbHelper.ServiceProvider.GetService(typeof(ISettingCenter));
            var ta = Task.Run(() => { configRetrieval = new ConfigRetrieval(_configuration, configManagerHandle, settingCenter); });
            ta.Wait();
            isdeletewordpath = Convert.ToBoolean(_configuration["appSettings:IsDeleteWordPath"]);
            IConfiguration configuration = _configuration;
            //进行配置分析服务的最大线程
            string state = "1";
            Dictionary<string, object> dicValue = new Dictionary<string, object>();
            //交互实体
            FileExtractModel fileExtractModel = new FileExtractModel();

            FillBackOptParameterModel fillBackOptParameter = JsonConvert.DeserializeObject<FillBackOptParameterModel>(message);
            fileExtractModel.MsgId = fillBackOptParameter.MsgId;
            fileExtractModel.CaseInfo = new LdtJudge.DataEngine.Model.Collection.CaseInfo();
            fileExtractModel.CaseInfo.OutRealtId = fillBackOptParameter.Outrelatid;
            fileExtractModel.CaseInfo.CaseId = fillBackOptParameter.CaseID;
            try
            {
                //RealizationClass realizationClass = new RealizationClass();
                //Ldt.DataEngin.Assemble.Main main = new Ldt.DataEngin.Assemble.Main();
                //main.configRetrieval = _configRetrieval;
                //main.Loading(fillBackOptParameter.CaseType, fillBackOptParameter.CourtID, fillBackOptParameter.Outrelatid, fillBackOptParameter.AppId, fillBackOptParameter.SourceCode, fillBackOptParameter.CCIDs, fillBackOptParameter.CaseID);
                //main.LogShow = new LogShow(realizationClass.ShowMessage);
                //main.ProcessShow = new ProcessShow(realizationClass.ProcessShow);
                //main.ModuleProcessShow = new ModuleProcessShow(realizationClass.SetModuleProcess);
                //main.configRetrieval = _configRetrieval;
                //uiTextBox5.Text = main.FillBackOptimization(fillBackOptParameter.Content, fillBackOptParameter.CaseType, fillBackOptParameter.CourtID, fillBackOptParameter.Outrelatid, fillBackOptParameter.AppId, fillBackOptParameter.SourceCode, fillBackOptParameter.laststage, fillBackOptParameter.MsgId, fillBackOptParameter.CCIDs);

                //fillBackOptParameter.MsgState = "1";
                //fileExtractModel.MsgState = "1";

            }
            catch (Exception ex)
            {
                fileExtractModel.MsgState = "0";
                state = "0";
                dicValue.Add("CaseID", fillBackOptParameter.CaseID);
                dicValue.Add("OutRelateId", fillBackOptParameter.Outrelatid);
                dicValue.Add("CaseTypeId", fillBackOptParameter.CaseType);
                dicValue.Add("State", state);
                dicValue.Add("MsgId", fillBackOptParameter.MsgId);
                fillBackOptParameter.MsgState = "0";
            }
            finally
            {



            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WordHandle wordHandle = new WordHandle();
            WebClient wb = new WebClient();
            byte[] bytes = wb.DownloadData("http://192.168.1.139:8080/fs/api/v1/download/683DE2DD-B57F-477B-9355-7FFDB73741D4");
            Stream stream = BytesToStream(bytes);

            
        }

        public Stream BytesToStream(byte[] bytes)
        {

            Stream stream = new MemoryStream(bytes);

            return stream;

        }
    }



    public class RootModel
    {
        /// <summary>
        /// 
        /// </summary>
        public List<ChangeDataModel> changedata { get; set; }
    }
}
